package com.koron.system.agent.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.bean.system.agent.po.AgentRoleUserPo;
import com.koron.bean.system.agent.po.AgentUserPo;
import com.koron.bean.system.agent.vo.AgentUserVo;
import com.koron.bean.system.role.RoleBean;
import com.koron.system.agent.bean.query.AgentUserQuery;
import com.koron.system.agent.mapper.AgentUserMapper;
import com.koron.system.agent.service.AgentRoleUserService;
import com.koron.system.agent.service.AgentUserService;
import com.koron.system.role.service.RoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 代理用户表(AgentUser)表服务实现类
 *
 * @author zhouj
 * @since 2021-11-23 17:45:40
 */
@Service("agentUserService")
public class AgentUserServiceImpl implements AgentUserService {

    @Autowired
    AgentUserMapper agentUserMapper;

    @Autowired
    AgentRoleUserService agentRoleUserService;

    @Autowired
    RoleService roleService;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(AgentUserPo agentUser) {

        return agentUserMapper.insertOrUpdate(agentUser);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(List<AgentUserPo> agentUserList) {

        return agentUserMapper.insertOrUpdateBatch(agentUserList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(AgentUserPo agentUser) {

        if (agentUser.getScope() != null && agentUser.getScope() == 1) {
            //全部授权 将领导的角色全部赋予代理
            List<RoleBean> roleBeans = roleService.queryUserRole(agentUser.getLeader());
            if (CollectionUtils.isNotEmpty(roleBeans)) {
                List<AgentRoleUserPo> roleUserPos = new ArrayList<>();
                roleBeans.forEach(p -> {
                    AgentRoleUserPo agentRoleUserPo = new AgentRoleUserPo();
                    agentRoleUserPo.setAgent(agentUser.getAgent());
                    agentRoleUserPo.setLeader(agentUser.getLeader());
                    agentRoleUserPo.setRoleId(p.getId());
                    roleUserPos.add(agentRoleUserPo);
                });

                //删除该代理用户角色数据
                agentRoleUserService.deleteByAgent(agentUser.getLeader(), agentUser.getAgent());
                agentRoleUserService.insertBatch(roleUserPos);
            }
        }
        return agentUserMapper.insert(agentUser);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(List<AgentUserPo> agentUserList) {

        return agentUserMapper.insertBatch(agentUserList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(AgentUserPo agentUser) {
        return agentUserMapper.update(agentUser);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(String id) {

        return agentUserMapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public AgentUserPo queryById(String id) {

        return agentUserMapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<AgentUserPo> queryList(AgentUserPo agentUser) {
        return agentUserMapper.queryList(agentUser);
    }

    @Override
    public int deleteByAgent(String leader, String agent) {
        return agentUserMapper.deleteByAgent(leader, agent);
    }

    @Override
    public List<AgentUserVo> queryVoList(AgentUserQuery userQuery) {
        return agentUserMapper.queryVoList(userQuery);
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(AgentUserPo agentUser, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());

        List<AgentUserPo> list = agentUserMapper.queryList(agentUser);

        return new PageInfo(list);
    }

}
